//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OptionletStripper1.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Termstructures/Volatility/Capfloor/CapFloorTermVolSurface.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletStripper.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Termstructures::Volatility::Capfloor;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (Cephei::QL::Termstructures::Volatility::Capfloor::ICapFloorTermVolSurface^ prm1, Cephei::QL::Indexes::IIborIndex^ index, Microsoft::FSharp::Core::FSharpOption<Double>^ switchStrikes, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt32>^ maxIter) : COptionletStripper(COptionletStripper1::typeid)
{
    CCapFloorTermVolSurface^ _Cprm1;
    CIborIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phOptionletStripper1 = NULL;
#endif
        _Cprm1 = safe_cast<CCapFloorTermVolSurface^> (prm1);
        _Cprm1->Lock();
        boost::shared_ptr<QuantLib::CapFloorTermVolSurface>& _prm1 = static_cast<boost::shared_ptr<QuantLib::CapFloorTermVolSurface>&> (_Cprm1->GetShared ()); 
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Rate _switchStrikes = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (switchStrikes) ? (QuantLib::Rate)ValueHelper::Convert (switchStrikes->Value) : Null<QuantLib::Rate>()); //4
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-6); //4
        QuantLib::Natural _maxIter = 
            (Microsoft::FSharp::Core::FSharpOption<UInt32>::IsSome::get (maxIter) ? (QuantLib::Natural)ValueHelper::Convert (maxIter->Value) : 100); //4
        _ppOptionletStripper1 = new boost::shared_ptr<QuantLib::OptionletStripper1> (new QuantLib::OptionletStripper1 ( _prm1,  _index,  _switchStrikes,  _accuracy,  _maxIter ));
        SetOptionletStripper (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper1));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (boost::shared_ptr<QuantLib::OptionletStripper1>& childNative, Object^ owner) : COptionletStripper(COptionletStripper1::typeid)
{
#ifdef HANDLE
	_phOptionletStripper1 = NULL;
#endif
	_ppOptionletStripper1 = &childNative;
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper1));
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (QuantLib::OptionletStripper1& childNative, Object^ owner) : COptionletStripper(COptionletStripper1::typeid)
{
#ifdef HANDLE
	_phOptionletStripper1 = NULL;
#endif
	_ppOptionletStripper1 = new boost::shared_ptr<QuantLib::OptionletStripper1> (&childNative);
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper1));
    _OptionletStripper1Owner = owner;
    _OptionletStripperOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (COptionletStripper1^ copy) : COptionletStripper(COptionletStripper1::typeid)
{
#ifdef HANDLE
	_phOptionletStripper1 = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOptionletStripper1 = new boost::shared_ptr<QuantLib::OptionletStripper1> (copy->GetShared());
        _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper1));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (System::Type^ t) : COptionletStripper(COptionletStripper1::typeid)
{
#ifdef HANDLE
	_phOptionletStripper1 = NULL;
#endif
	if (!t->IsSubclassOf(COptionletStripper1::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (QuantLib::Handle<QuantLib::OptionletStripper1>& childNative, Object^ owner)  : COptionletStripper(COptionletStripper1::typeid)
{
	_phOptionletStripper1 = &childNative;
	_ppOptionletStripper1 = &static_cast<boost::shared_ptr<QuantLib::OptionletStripper1>>(childNative.currentLink());
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper1));
    _OptionletStripper1Owner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (QuantLib::Handle<QuantLib::OptionletStripper1> childNative)  : COptionletStripper(COptionletStripper1::typeid)
{
	_phOptionletStripper1 = &childNative;
	_ppOptionletStripper1 = &static_cast<boost::shared_ptr<QuantLib::OptionletStripper1>>(childNative.currentLink());
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper1));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::COptionletStripper1 (QuantLib::OptionletStripper1 childNative)  : COptionletStripper(COptionletStripper1::typeid)
{
#ifdef HANDLE
	_phOptionletStripper1 = NULL;
#endif
	_ppOptionletStripper1 = new boost::shared_ptr<QuantLib::OptionletStripper1> (new QuantLib::OptionletStripper1 (childNative));
    _ppOptionletStripper = new boost::shared_ptr<QuantLib::OptionletStripper> (boost::dynamic_pointer_cast<QuantLib::OptionletStripper> (*_ppOptionletStripper1));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::~COptionletStripper1 ()
{
    if (_ppOptionletStripper1 != NULL)
    {
	    delete _ppOptionletStripper1;
        _ppOptionletStripper1 = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::!COptionletStripper1 ()
{
    if (_ppOptionletStripper1 != NULL)
    {
	    delete _ppOptionletStripper1;
    }
}
QuantLib::OptionletStripper1& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::GetReference ()
{
    if (_ppOptionletStripper1 == NULL) throw gcnew NativeNullException ();
	return **_ppOptionletStripper1;
}
boost::shared_ptr<QuantLib::OptionletStripper1>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::GetShared ()
{
    if (_ppOptionletStripper1 == NULL) throw gcnew NativeNullException ();
	return *_ppOptionletStripper1;
}
QuantLib::OptionletStripper1* Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::GetPointer ()
{
    if (_ppOptionletStripper1 == NULL) throw gcnew NativeNullException ();
	return &**_ppOptionletStripper1;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OptionletStripper1>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::GetHandle ()
{
	if (_phOptionletStripper1 == NULL)
	{
		_phOptionletStripper1 = new Handle<QuantLib::OptionletStripper1> (*_ppOptionletStripper1);
	}
	return *_phOptionletStripper1;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::HasNative () 
{
	return (_ppOptionletStripper1 != NULL);
}

Cephei::QL::Math::IMatrix^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::CapFloorPrices::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppOptionletStripper1)->capFloorPrices ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::CapFloorVolatilities::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppOptionletStripper1)->capFloorVolatilities ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::OptionletPrices::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppOptionletStripper1)->optionletPrices ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletStripper1^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::PerformCalculations::get ()
{
    try
    {
    	(*_ppOptionletStripper1)->performCalculations ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1::SwitchStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppOptionletStripper1)->switchStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletStripper1^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletStripper1_Factory::Create (Cephei::QL::Termstructures::Volatility::Capfloor::ICapFloorTermVolSurface^ prm1, Cephei::QL::Indexes::IIborIndex^ index, Microsoft::FSharp::Core::FSharpOption<Double>^ switchStrikes, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt32>^ maxIter)
{
    return gcnew COptionletStripper1 ( prm1,  index,  switchStrikes,  accuracy,  maxIter);
}
